<template>
  <div class="app-container">
    <!--工具栏-->
    <div class="head-container">
      <div v-if="crud.props.searchToggle">
        <!-- 搜索 -->
        <label class="el-form-item-label">小区</label>
        <el-select v-model="query.xqId" style="width: 185px;" placeholder="请选择小区" class="filter-item" @keyup.enter.native="crud.toQuery">
          <el-option
            v-for="item in xqOptions"
            :key="item.value"
            :label="item.label"
            :value="item.value">
          </el-option>
        </el-select>
        <rrOperation :crud="crud" />
      </div>
      <!--如果想在工具栏加入更多按钮，可以使用插槽方式， slot = 'left' or 'right'-->
      <crudOperation :permission="permission" />
      <!--表单组件-->
      <el-dialog :close-on-click-modal="false" :before-close="crud.cancelCU" :visible.sync="crud.status.cu > 0" :title="crud.status.title" width="1000px">
        <el-form ref="form" :model="form" :rules="rules" size="small" label-width="80px">
          <el-form-item label="新闻标题">
            <el-input v-model="form.title" style="width: 370px;" />
          </el-form-item>
          <el-form-item label="新闻内容">
            <!--            <el-input v-model="form.content" :rows="3" type="textarea" style="width: 370px;" />-->
            <div style="border: 1px solid #ccc; margin-top: 10px;" v-if="flag">
              <!-- 工具栏 -->
              <Toolbar
                style="border-bottom: 1px solid #ccc"
                :editor="editor"
                :default-config="toolbarConfig"
              />
              <!-- 编辑器 -->
              <Editor
                v-model="html"
                style="height: 400px; overflow-y: hidden;"
                :default-config="editorConfig"
                @onChange="onChange"
                @onCreated="onCreated"
              />
            </div>
          </el-form-item>
        </el-form>
        <div slot="footer" class="dialog-footer">
          <el-button type="text" @click="crud.cancelCU">取消</el-button>
          <el-button :loading="crud.status.cu === 2" type="primary" @click="crud.submitCU">确认</el-button>
        </div>
      </el-dialog>
      <!--表格渲染-->
      <el-table ref="table" v-loading="crud.loading" :data="crud.data" size="small" style="width: 100%;" @selection-change="crud.selectionChangeHandler">
        <el-table-column type="selection" width="55" />
        <el-table-column prop="title" label="新闻标题" />
<!--        <el-table-column prop="content" label="新闻内容" />-->
        <el-table-column prop="createTime" label="创建日期" />
        <el-table-column v-if="checkPer(['admin','news:edit','news:del'])" label="操作" width="150px" align="center">
          <template slot-scope="scope">
            <udOperation
              :data="scope.row"
              :permission="permission"
            />
          </template>
        </el-table-column>
      </el-table>
      <!--分页组件-->
      <pagination />
    </div>
  </div>
</template>

<script>
import crudNews from '@/api/system/news'
import CRUD, { presenter, header, form, crud } from '@crud/crud'
import rrOperation from '@crud/RR.operation'
import crudOperation from '@crud/CRUD.operation'
import udOperation from '@crud/UD.operation'
import pagination from '@crud/Pagination'
import { Editor, Toolbar } from '@wangeditor/editor-for-vue'
import { mapGetters } from 'vuex'
import { getToken } from '@/utils/auth'
import { allList } from '@/api/system/regionManage'

const defaultForm = { id: null, createTime: null, updateTime: null, createBy: null, updateBy: null, deleted: null, xqId: null, title: null, content: null }
export default {
  name: 'News',
  components: { pagination, crudOperation, rrOperation, udOperation, Editor, Toolbar },
  mixins: [presenter(), header(), form(defaultForm), crud()],
  cruds() {
    return CRUD({ title: '新闻咨询', url: 'api/news', idField: 'id', sort: 'id,desc', crudMethod: { ...crudNews }})
  },
  data() {
    return {
      permission: {
        add: ['admin', 'news:add'],
        edit: ['admin', 'news:edit'],
        del: ['admin', 'news:del']
      },
      rules: {
      },
      queryTypeOptions: [
        { key: 'xqId', display_name: '小区id' }
      ],
      editor: null,
      html: '',
      toolbarConfig: {
        // toolbarKeys: [ /* 显示哪些菜单，如何排序、分组 */ ],
        // excludeKeys: [ /* 隐藏哪些菜单 */ ],
      },
      editorConfig: {
        placeholder: '请输入内容...',
        // autoFocus: false,
        // 所有的菜单配置，都要在 MENU_CONF 属性下
        MENU_CONF: {
          uploadImage: {
            server: '/api/localStorage/wangeditor',
            headers: { 'Authorization': getToken() },
            fieldName: 'file'
          },
          uploadVideo: {
            server: '/api/localStorage/wangeditor',
            headers: { 'Authorization': getToken() },
            fieldName: 'file'
          },
          withCredentials: true
        }
      },
      mode: 'default', // or 'simple'
      flag: false,
      xqOptions: []
    }
  },
  computed: {
    ...mapGetters([
      'baseApi',
      'fileUploadApi'
    ])
  },
  created() {
    allList().then(data => {
      if (data && data.data) {
        for (const item of data.data) {
          this.xqOptions.push({ 'value': item.id, 'label': item.communityName })
        }
      }
    })
  },
  beforeDestroy() {
    const editor = this.editor
    if (editor == null) return
    editor.destroy() // 组件销毁时，及时销毁 editor ，重要！！！
  },
  methods: {
    // 钩子：在获取表格数据之前执行，false 则代表不获取数据
    [CRUD.HOOK.beforeRefresh]() {
      this.crud.optShow.download = false
      return true
    },
    onCreated(editor) {
      this.editor = Object.seal(editor) // 【注意】一定要用 Object.seal() 否则会报错
    },
    onChange(editor) {
      this.html = editor.getHtml()
      // console.log('onChange', editor.getHtml()) // onChange 时获取编辑器最新内容
    },
    // 提交保存之后
    [CRUD.HOOK.beforeToAdd]() {
      this.html = ''
      this.flag = true
      return true
    },
    // 提交保存之后
    [CRUD.HOOK.afterSubmit]() {
      this.html = ''
      this.flag = false
      return true
    },
    // 编辑之前
    [CRUD.HOOK.beforeToEdit](obj, row) {
      this.html = row.content
      this.flag = true
      return true
    },
    // 取消添加之前
    [CRUD.HOOK.beforeAddCancel]() {
      this.html = ''
      this.flag = false
      return true
    },
    // 取消编辑之前
    [CRUD.HOOK.beforeEditCancel]() {
      this.html = ''
      this.flag = false
      return true
    },
    // 提交前
    [CRUD.HOOK.beforeSubmit]() {
      this.crud.form.content = this.html
      return true
    }
  }
}
</script>

<style src="@wangeditor/editor/dist/css/style.css"></style>
